<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require('../config/config.php');
	require('../class/function.php');
	require('../class/autoload.php');
	session_start();



	//获取验证码
	if($_REQUEST['type'] == 'get_code'){
		require('../class/plugin/vcode.class.php');
		Code\VCode::GetCode();
		exit;
	}


	//生成二维码图片
	if($_REQUEST['type'] == 'erweima'){
		$url='http://www.bm123.top/nav.html?id=a1234';
		get_erweima($url);
	}



	//===== ### 请求token

	if($_REQUEST['type'] == 'token'){
		echo $token = md5(time()).md5(rand());
		$_SESSION[$_REQUEST['token_name']]= $token;
		exit;
	}





	//===== ### 验证token

	$token_arr = array(		// 跳过token验证的type项 添加到这里

	);
	if(!in_array($_REQUEST['type'],$token_arr)){
		if(!@$_REQUEST['remote_token']){	//验证本地token
			if(!@$_REQUEST['token'] || $_REQUEST['token'] != $_SESSION[$_REQUEST['type']]){
				$_REQUEST['token'] = md5(rand());
				exit('10001');
			}
			$_REQUEST['token'] = md5(rand());
		}else{		//验证远程token
			if(!CheckRemoteToken($_REQUEST['remote_token'])){
				exit('10002');	//远程token 验证失败
			}
		}
	}


	switch ($_REQUEST['type']) {

		default :
			echo 'default';
			break;

		//获取管理员的对县的权限列表
		case 'get_admin_user_perm_list':
			$user_id = I('user_id','p');
			$pdo = MyPdoPublic::init();
			$sql = 'select perm from admin_user where id = ?';
			$res = $pdo->dql($sql,array($user_id),1);
			if($res){
				echo $res['perm'];
			}else{
				echo '[]';
			}
			break;


		//设置管理员的县管理权限
		case 'set_admin_perm':
			$city_id_list = I('city_id_list','p');
			$user_id = I('user_id','p');
			if(!$city_id_list || !$user_id){
				exit('err1');
			}

			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set perm = ? where id = ?';
			$res = $pdo->dml($sql,array($city_id_list,$user_id));
			if($res){
				exit('ok');
			}else{
				exit('err2');
			}
			break;



		//更改管理成员状态 
		case 'change_admin_user_stat':
			$id = I('id','p');
			if(!$id){
				exit('err1');
			}
			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set stat=if((stat+1)>2,1,2) where id = ?';
			$res = $pdo->dml($sql,array($id));
			if($res){
				exit('ok');
			}else{
				exit('err2');
			}
			break;


		//删除管理成员
		case 'del_admin_user':
			$id = I('id','p');
			if(!$id){
				exit('err1');
			}
			$pdo = MyPdoPublic::init();
			$sql = 'delete from admin_user where id = ?';
			$res = $pdo->dml($sql,array($id));
			if($res){
				exit('ok');
			}else{
				exit('err2');
			}
			break;


		//获取管理成员列表
		case 'get_admin_user_list':
			$pdo = MyPdoPublic::init();
			$pnow = I('pnow','p');
			$psize = I('psize','p');
			$search = I('search','p');

			$start = ($pnow-1) * $psize;

			if($search){
				$sql = "select id,user,stat from admin_user where user like ? limit {$start},{$psize}";
				$arr = array("%{$search}%");
			}else{
				$sql = "select id,user,stat from admin_user limit {$start},{$psize}";
				$arr = array();
			}
			$res = $pdo->dql($sql,$arr);
			if($res){
				echo json_encode($res);
			}
			break;


		//添加后台管理成员
		case 'add_admin_user':
			$user = I('user','p');
			if(!$user){
				exit('err1');
			}
			$pdo = MyPdoPublic::init();
			$sql = 'select id from admin_user where user = ?';
			$res = $pdo->dql($sql,array($user));
			if(!$res){
				$sql2 = 'insert into admin_user values(null,?,md5("123"),"",1)';
				$res2 = $pdo->dml($sql2,array($user));
				exit('ok');
			}else{
				exit('err2'); 	//用户已存在！
			}
			break;


		//设置开通城市的库址库名
		case 'set_open_city':
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			$city_id = I('city_id','p');
			if(!$city_id){
				exit('err1');
			}
			$pdo = MyPdoPublic::init();
			$sql = 'update city set db_host=?,db_name=? where id=?';
			$res = $pdo->dml($sql,array(IpEncrypt($db_host),$db_name,$city_id));
			if($res){
				get_open_city($config);
				//$cache_url = '/open_city/open_city.html';
				//UpyunAddFile($config,$cache_url,'none');   //清空开通城市缓存数据
				exit('ok');
			}
			break;



		//获取开通城市信息
		case 'get_open_city_list':
			echo $city_list = get_open_city($config);
			break;


		//获取全部城市信息
		case 'get_city_data':
			$pdo = MyPdoPublic::init();
			$sql = 'select id,code,name,parent_id,level,db_host,db_name from city order by id';
			$res = $pdo->dql($sql,array());
			if($res){
				$data = array();
				foreach($res as $v){
					$v['db_host'] = $v['db_host']?IpDecrypt($v['db_host']):$v['db_host'];
					$arr = array(
						$v['id'],
						$v['parent_id'],
						$v['level'],
						$v['code'],
						$v['name'],
						$v['db_host'],
						$v['db_name']
					);
					$data[] = $arr;
				}
				echo json_encode($data);
			}
			break;



		//删除开通服务器域名
		case 'del_domain':
			$ids = I('domain','p');
			$ids = explode(',', $ids);
			$arr = array();
			$ident = '';
			foreach($ids as $v){
				$arr[] = $v;
				$ident .="?,";
			}
			$ident = trim($ident,',');
			$pdo = MyPdoPublic::init();
			$sql = "delete from open_city_list where id in ($ident)";
			$res = $pdo->dml($sql,$arr);
			if($res){
				exit('ok');
			}
			break;


		//添加开通服务器域名
		case 'add_domian':
			$domain = I('domain','p');
			if(!$domain){
				exit;
			}
			$pdo = MyPdoPublic::init();
			$sql = 'select id from open_city_list where domain = ?';
			$res = $pdo->dql($sql,array($domain));
			if(!$res){
				$sql2 = 'insert into open_city_list values(null,?)';
				$res2 = $pdo->dml($sql2,array($domain));
				if($res2){
					echo 'ok';
				}
			}else{
				echo 'err';
			}
			exit;
			break;

		//获取开通的域名列表
		case 'get_domain_list':
			$pdo = MyPdoPublic::init();
			$sql = 'select id,domain from open_city_list order by id desc';
			$res = $pdo->dql($sql,array());
			if($res){
				foreach($res as $v){
					$list[] = [$v['id'],$v['domain']];
					$list2[] = $v['domain'];
				}
				echo json_encode($list);
				//添加到upyun的数据不能带ip，只供前台使用
				$list3 = json_encode($list2);
			}else{
				$list3 = '[]';
			}
			UpyunAddFile($config,'/server_list/server_list.html',$list3);
			break;



		//公共数据库设置
		case 'public_database_set':
			$db_addr = I('db_addr','p');
			$db_name = I('db_name','p');
			if(!$db_addr || !$db_name){
				exit('err1');	//缺少必填项
			}
			$arr = array(
				$db_name,
				$db_addr
			);
			$res = json_encode($arr);
			$a = UpyunAddFile($config,'/public_database/public.html',$res);
			exit($res);
			break;

		//获取仅供数据库信息
		case 'get_public_database':
			//$ourl = $config['upyun_url'].'/public_database/public.html?a='.rand();
			//echo $res = CurlForGet($ourl);
			echo GetPublicDatabase();
			break;



		//登陆
		case 'login':
			$user = I('user','p');
			$pwd = I('pwd','p');
			$code = I('code','p');
			if(!$user || !$pwd || !$code){
				exit('err1');	//缺少参数
			}

			if(!CheckCode($code)){
				//exit('err2');	//验证码不正确
			}

			if($user == 'admin'){
				if($pwd != 'a'){
					exit('err3');	//用户名或密码不正确
				}
			}else{
				exit('err3');
			}
			exit('ok');
			break;





		
	}









/*
	### 检测验证码是否正确
	参一：code  要比对的验证码
	返回：正确返回true,失败返回false
*/
function CheckCode($code){
	require('../class/plugin/vcode.class.php');
	if(Code\VCode::Check_Code($code)){
		return true;
	}else{
		return false;
	}
}


